package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.reggie.bean.Orders;
import com.heima.reggie.bean.PageParam;
import com.heima.reggie.bean.User;
import com.heima.reggie.common.R;
import com.heima.reggie.dto.OrdersDto;
import com.heima.reggie.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;

@RequestMapping("/order")
@RestController
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @ApiOperation(value ="添加订单")
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders, HttpSession session) {
        User user = (User) session.getAttribute("user");
        int i = ordersService.add(orders,user.getId());
        if (i > 0) {
            return R.success("添加订单成功！");
        } else {
            return R.error("添加订单失败！");
        }
    }

    @GetMapping("/page")
    @ApiOperation(value ="按条件分页查询")
    public R findPage(PageParam pageParam) {
        System.out.println("--------");
        System.out.println(pageParam);
        IPage<OrdersDto> page=ordersService.findPage(pageParam);
        if (page != null) {
            return R.success(page);
        } else {
            return R.error("查询失败！");
        }
    }
    @ApiOperation(value ="按用户分页查询")
    @GetMapping("/userPage")
    public R findOrdersUserPage(PageParam pageParam, HttpSession session) {
        User user = (User) session.getAttribute("user");
        IPage<OrdersDto> page=ordersService.findOrdersUserPage(pageParam,user.getId());
        if (page != null) {
            return R.success(page);
        } else {
            return R.error("查询失败！");
        }
    }
}
